from math import e
import random
import time

class FightingGame:
    def __init__(self):
        self.player = {
            "user": '玩家', "hp": 1000, "sp": 0, "skill_points": 0,
            "unstable": False, "dizzy": False, "prone": False,
            "dodging": False, "hegemony": False
        }
        self.computer = {
            "user": '电脑', "hp": 1000, "sp": 0, "skill_points": 0,
            "unstable": False, "dizzy": False, "prone": False,
            "dodging": False, "hegemony": False
        }
        self.round = 1
        #记录连续有效攻击次数，超过3次造成对方眩晕状态，中断下回合动作
        self.effective_attack_player = 0
        self.effective_attack_computer = 0

    def calculate_damage(self, attack_type, is_skill=False):
        """统一返回 (damage, hit_rate)"""
        base_dmg = {
            'head': (50, 100),
            'body': (30, 80),
            'legs': (10, 60)
        }

        if is_skill:
            if attack_type == 'heavy':
                base = base_dmg['body']
                dmg = max(200, int(random.randint(*base) * 4))
            elif attack_type == 'elbow':
                base = base_dmg['head']
                dmg = max(140, int(random.randint(*base) * 2.5))
            elif attack_type == 'sweep':
                base = base_dmg['legs']
                dmg = max(50, int(random.randint(*base) * 2.5))
            else:
                dmg = 0
            return (dmg, 80) 

        dmg = random.randint(*base_dmg[attack_type])
        hit_rate = {
            'head': 60,
            'body': 80,
            'legs': 70
        }[attack_type]

        return (dmg, hit_rate)

    def apply_effects(self, target, effect_type):
        """应用特殊效果（不稳/眩晕/倒地/霸体）"""
        if effect_type == 'unstable':
            target['unstable'] = True
        elif effect_type == 'dizzy':
            target['dizzy'] = True
        elif effect_type == 'prone':
            target['prone'] = True
        elif effect_type == 'dodging':
            target['dodging'] = True
        elif effect_type == 'hegemony':
            target['hegemony'] = True

    def resume_effects(self, target, effect_type):
        """恢复特殊效果（不稳/眩晕/倒地/霸体）"""
        if effect_type == 'unstable':
            target['unstable'] = False
        elif effect_type == 'dizzy':
            target['dizzy'] = False
        elif effect_type == 'prone':
            target['prone'] = False
        elif effect_type == 'dodging':
            target['dodging'] = False
        elif effect_type == 'hegemony':
            target['hegemony'] = False
            
    def get_available_actions(self, character):
        """获取可用动作列表"""
        if character['prone']:
            return ['rise']
        actions = ['attack', 'defend']
        if character['skill_points'] > 0:
            actions.append('skill')
        return actions

    def execute_action(self, player_action, computer_action, action_num):
        """执行单个子回合动作"""
        print(f"\n[动作{action_num}结算]")
        time.sleep(1)
 
        # 闪避优先
        if player_action.get('type') == 'dodge' or computer_action.get('type') == 'dodge':
            self.handle_dodge(player_action, computer_action)

        # 重击优先
        if player_action.get('type') == 'heavy' or computer_action.get('type') == 'heavy':
            self.handle_heavy(player_action, computer_action)

        # 肘击优先
        if player_action.get('type') == 'elbow' or computer_action.get('type') == 'elbow':
            self.handle_elbow(player_action, computer_action)

        # 扫腿优先
        if player_action.get('attack_type') == 'legs' or computer_action.get('attack_type') == 'legs' or player_action.get('type') == 'sweep' or computer_action.get('type') == 'sweep':
            self.handle_leg_attack(player_action, computer_action)

        # 普通攻击
        self.handle_normal_attack(player_action, computer_action)

    def handle_dodge(self, p_action, c_action):
        """处理闪避"""
        if p_action.get('type') == 'dodge':
            self.player['skill_points'] -= 1
            self.effective_attack_player = 0
            self.apply_effects(self.player, 'dodging')
            print("玩家使用闪避！尝试躲避当前动作")
            time.sleep(1)
            self.update_sp(self.player, True, "M")
        if c_action.get('type') == 'dodge':
            self.computer['skill_points'] -= 1
            self.effective_attack_computer = 0
            self.apply_effects(self.computer, 'dodging')
            print("电脑使用闪避！尝试躲避当前动作")
            time.sleep(1)
            self.update_sp(self.computer, True, "M")

    def fun_dodge(self, p_action, c_action):
        """处理闪避返回"""
        describe_actions = {
            'heavy': '技能重击',
            'elbow': '技能肘击',
            'sweep': '技能扫腿'
        }
        p_action_type = p_action.get('type', '')
        describe_p_action = describe_actions.get(p_action_type.lower(), '普攻')
        c_action_type = c_action.get('type', '')
        describe_c_action = describe_actions.get(c_action_type.lower(), '普攻')

        p_def = p_action.get('type') == 'defend'
        c_def = c_action.get('type') == 'defend'

        if self.player['dodging'] == True:
            if c_def:
                print(f"玩家尝试闪避电脑，电脑做出防御姿势")
                self.update_sp(self.computer, True, "S")
                self.effective_attack_computer = 0
            else:
                print(f"玩家闪避了电脑{describe_c_action}的攻击")
                self.effective_attack_computer = 0
            time.sleep(1)
            self.resume_effects(self.player, 'dodging')
        
        if self.computer['dodging'] == True:
            if p_def:
                print(f"电脑尝试闪避玩家，玩家做出防御姿势")
                self.update_sp(self.player, True, "S")
                self.effective_attack_player = 0
            else:
                print(f"电脑闪避了玩家{describe_p_action}的攻击")
                self.effective_attack_player = 0
            time.sleep(1)
            self.resume_effects(self.computer, 'dodging')

    def handle_heavy(self, p_action, c_action):
        """处理重击"""
        ps_hit = p_action.get('type') == 'heavy'
        cs_hit = c_action.get('type') == 'heavy'

        p_def = p_action.get('type') == 'defend'
        c_def = c_action.get('type') == 'defend'

        if ps_hit:
            self.player['skill_points'] -= 1
        if cs_hit:
            self.computer['skill_points'] -= 1

        if self.player['dodging'] == True or self.computer['dodging'] == True:
            self.fun_dodge(p_action, c_action)
            return
        
        if ps_hit and cs_hit:
            p_dmg, p_hit_rate = self.calculate_damage('heavy', is_skill=True)
            c_dmg, c_hit_rate = self.calculate_damage('heavy', is_skill=True)

            self.apply_effects(self.player, 'hegemony')
            self.apply_effects(self.computer, 'hegemony')

            p_hit = random.random() * 100 < p_hit_rate
            if not p_hit:
                print(f"玩家重击未命中！")
                time.sleep(1)
                self.effective_attack_player = 0
            else:
                self.computer['hp'] -= p_dmg
                print(f"玩家重击命中！造成{p_dmg}伤害")
                time.sleep(1)
                self.effective_attack_player += 1
            
            c_hit = random.random() * 100 < c_hit_rate
            if not c_hit:
                print(f"电脑重击未命中！")
                time.sleep(1)
                self.effective_attack_computer = 0
            else:
                self.player['hp'] -= c_dmg
                print(f"电脑重击命中！造成{c_dmg}伤害")
                time.sleep(1)
                self.effective_attack_computer += 1
            
            return

        if ps_hit:
            p_dmg, p_hit_rate = self.calculate_damage('heavy', is_skill=True)
            p_hit = random.random() * 100 < p_hit_rate
            self.apply_effects(self.player, 'hegemony')
            if not p_hit:
                print(f"玩家重击未命中！")
                time.sleep(1)
                self.effective_attack_player = 0
            else:
                self.computer['hp'] -= p_dmg
                print(f"玩家重击命中！造成{p_dmg}伤害")
                time.sleep(1)
                self.effective_attack_player += 1
                if c_def:
                    self.update_sp(self.computer, True, "S")
                    self.effective_attack_computer = 0
                    print(f"电脑无法防御重击")
                    time.sleep(1)
                        
        if cs_hit:
            c_dmg, c_hit_rate = self.calculate_damage('heavy', is_skill=True)
            c_hit = random.random() * 100 < c_hit_rate
            self.apply_effects(self.computer, 'hegemony')
            if not c_hit:
                print(f"电脑重击未命中！")
                time.sleep(1)
                self.effective_attack_computer = 0
            else:
                self.player['hp'] -= c_dmg
                print(f"电脑重击命中！造成{c_dmg}伤害")
                time.sleep(1)
                self.effective_attack_computer += 1
            if p_def:
                self.update_sp(self.player, True, "S")
                self.effective_attack_player = 0
                print(f"玩家无法防御重击")
                time.sleep(1)  
                
    def handle_elbow(self, p_action, c_action):
        """处理肘击"""
        ps_hit = p_action.get('type') == 'elbow'
        cs_hit = c_action.get('type') == 'elbow'
        p_defend = p_action.get('defend_type') == 'head'
        c_defend = c_action.get('defend_type') == 'head'
        p_def = p_action.get('type') == 'defend'
        c_def = c_action.get('type') == 'defend'
        
        if ps_hit:
            self.player['skill_points'] -= 1
        if cs_hit:
            self.computer['skill_points'] -= 1

        if self.player['dodging'] == True or self.computer['dodging'] == True:
            self.fun_dodge(p_action, c_action)
            return

        if ps_hit and cs_hit:
            p_dmg, p_hit_rate = self.calculate_damage('elbow', is_skill=True)
            c_dmg, c_hit_rate = self.calculate_damage('elbow', is_skill=True)
            
            p_hit = random.random() * 100 < p_hit_rate
            if not p_hit:
                print(f"玩家肘击未命中！")
                time.sleep(1)
                self.effective_attack_player = 0
            else:
                self.computer['hp'] -= p_dmg
                print(f"玩家肘击命中！造成{p_dmg}伤害")
                time.sleep(1)
                self.effective_attack_player += 1
            
            c_hit = random.random() * 100 < c_hit_rate
            if not c_hit:
                print(f"电脑肘击未命中！")
                time.sleep(1)
                self.effective_attack_computer = 0
            else:
                self.player['hp'] -= c_dmg
                print(f"电脑肘击命中！造成{c_dmg}伤害")
                time.sleep(1)
                self.effective_attack_computer += 1

            return

        if ps_hit:
            p_dmg, p_hit_rate = self.calculate_damage('elbow', is_skill=True)
            p_hit = random.random() * 100 < p_hit_rate
            if not p_hit:
                print(f"玩家肘击未命中！")
                time.sleep(1)
                self.effective_attack_player = 0
            elif c_defend:
                p_dmg = p_dmg//2
                self.computer['hp'] -= p_dmg
                print(f"玩家肘击被电脑格挡了！仅造成{p_dmg}伤害")
                time.sleep(1)
                print(f"电脑格挡成功回复sp")
                time.sleep(1)
                self.update_sp(self.computer, True, "L")
                self.effective_attack_computer = 0
                self.effective_attack_player = 0
            else: 
                self.computer['hp'] -= p_dmg
                self.effective_attack_player += 1
                if self.computer['hegemony'] == True:
                    print(f"电脑霸体状态，不会被中断")
                    time.sleep(1)
                    print(f"玩家肘击命中！造成{p_dmg}伤害")
                    time.sleep(1)
                    self.resume_effects(self.computer, 'hegemony')
                else:
                    self.apply_effects(self.computer, 'dizzy')
                    print(f"玩家肘击命中！造成{p_dmg}伤害并中断电脑的进攻")
                    time.sleep(1)
                    self.effective_attack_computer = 0
                    if c_def:
                        print(f"电脑防御失败")
                        time.sleep(1)
                        self.update_sp(self.computer, True, "S")

        if cs_hit:
            c_dmg, c_hit_rate = self.calculate_damage('elbow', is_skill=True)
            c_hit = random.random() * 100 < c_hit_rate
            if not c_hit:
                print(f"电脑肘击未命中！")
                time.sleep(1)
                self.effective_attack_computer = 0
            elif p_defend:
               c_dmg = c_dmg//2
               self.player['hp'] -= c_dmg
               print(f"电脑肘击被玩家格挡了！仅造成{c_dmg}伤害")
               time.sleep(1)
               print(f"玩家格挡成功回复sp")
               time.sleep(1)
               self.update_sp(self.player, True, "L")
               self.effective_attack_player = 0
               self.effective_attack_computer = 0
            else: 
                self.player['hp'] -= c_dmg
                self.effective_attack_computer += 1
                if self.player['hegemony'] == True:
                    print(f"玩家霸体状态，不会被中断")
                    time.sleep(1)
                    print(f"电脑肘击命中！造成{c_dmg}伤害")
                    time.sleep(1)
                    self.resume_effects(self.player, 'hegemony')
                else:
                    self.apply_effects(self.player, 'dizzy')
                    print(f"电脑肘击命中！造成{c_dmg}伤害并中断玩家的进攻")
                    time.sleep(1)
                    self.effective_attack_player = 0
                    if p_def:
                        print(f"玩家防御失败")
                        time.sleep(1)
                        self.update_sp(self.player, True, "S")

    def handle_leg_attack(self, p_action, c_action):
        """处理腿部攻击（扫腿）"""
        ps_hit = p_action.get('type') == 'sweep'
        cs_hit = c_action.get('type') == 'sweep'
        if ps_hit:
            p_leg = True
            self.player['skill_points'] -= 1
        else:
            p_leg = p_action.get('attack_type') == 'legs'
        if cs_hit:
            c_leg = True
            self.computer['skill_points'] -= 1
        else:
            c_leg = c_action.get('attack_type') == 'legs'
        p_defend = p_action.get('defend_type') == 'legs'
        c_defend = c_action.get('defend_type') == 'legs'
        p_def = p_action.get('type') == 'defend'
        c_def = c_action.get('type') == 'defend'

        if p_leg or c_leg:
            if ps_hit and not self.computer['hegemony'] and not self.player['dizzy']:
                self.apply_effects(self.computer, 'prone')
                print(f"玩家使用技能扫腿！")
                time.sleep(1)
            elif not ps_hit and not cs_hit and p_leg and not self.computer['hegemony'] and not self.player['dizzy']:
                self.apply_effects(self.computer, 'unstable')
                print(f"电脑获得不稳状态")
                time.sleep(1)

            if cs_hit and not self.player['hegemony'] and not self.computer['dizzy']:
                self.apply_effects(self.player, 'prone')
                print(f"电脑使用技能扫腿！")
                time.sleep(1)
            elif not cs_hit and not ps_hit and c_leg and not self.player['hegemony'] and not self.computer['dizzy']:
                self.apply_effects(self.player, 'unstable')
                print(f"玩家获得不稳状态")
                time.sleep(1)

        if p_leg:
            if self.player['dizzy']:
                self.resume_effects(self.player, 'dizzy')
                print(f"玩家因眩晕状态中断腿部攻击")
                time.sleep(1)
            else:
                if  self.computer['dodging']:
                    self.resume_effects(self.computer, 'dodging')
                    print(f"玩家识破电脑的闪避，进行腿部攻击")
                    time.sleep(1)
                if  ps_hit:
                    p_dmg, p_hit_rate = self.calculate_damage('sweep', is_skill=True)
                    if self.player['unstable']:
                        p_hit_rate -= 20
                        self.resume_effects(self.player, 'unstable')
                    p_hit = random.random() * 100 < p_hit_rate
                    if not p_hit:
                        print(f"玩家腿部技能未命中！")
                        time.sleep(1)
                        self.effective_attack_player = 0
                        self.resume_effects(self.computer, 'prone')
                        if c_defend:
                            print(f"电脑通过防御完美躲过攻击")
                            time.sleep(1)
                            self.update_sp(self.computer, True, "L")
                            self.effective_attack_computer = 0
                        elif c_def:
                            print(f"电脑处于防御姿势回复sp")
                            time.sleep(1)
                            self.update_sp(self.computer, True, "S")
                            self.effective_attack_computer = 0
                    elif c_defend:
                        p_dmg = p_dmg//2
                        self.computer['hp'] -= p_dmg
                        print(f"玩家扫腿被电脑格挡了！仅造成{p_dmg}伤害")
                        time.sleep(1)
                        print(f"电脑格挡成功回复sp")
                        time.sleep(1)
                        self.update_sp(self.computer, True, "L")
                        self.effective_attack_computer = 0
                        self.effective_attack_player = 0
                        self.resume_effects(self.computer, 'prone')
                    else: 
                        self.computer['hp'] -= p_dmg
                        self.effective_attack_player += 1
                        if self.computer['hegemony'] == True:
                            print(f"电脑霸体状态，不会倒地")
                            time.sleep(1)
                            print(f"玩家扫腿命中！造成{p_dmg}伤害")
                            time.sleep(1)
                            self.resume_effects(self.computer, 'hegemony')
                            self.resume_effects(self.computer, 'prone')
                        else:
                            print(f"玩家扫腿命中！造成{p_dmg}伤害并使电脑倒地")
                            time.sleep(1)
                            if c_def:
                                print(f"电脑防御失败")
                                time.sleep(1)
                                self.update_sp(self.computer, True, "S")
                                self.effective_attack_computer = 0
                else:
                    p_dmg, p_hit_rate = self.calculate_damage('legs', is_skill=False)
                    if self.player['unstable']:
                        p_hit_rate -= 20
                        self.resume_effects(self.player, 'unstable')
                    p_hit = random.random() * 100 < p_hit_rate
                    if not p_hit:
                        print(f"玩家腿部攻击未命中！")
                        time.sleep(1)
                        self.effective_attack_player = 0
                        self.resume_effects(self.computer, 'unstable')
                        if c_defend:
                            print(f"电脑通过防御完美躲过攻击")
                            time.sleep(1)
                            self.update_sp(self.computer, True, "L")
                            self.effective_attack_computer = 0
                        elif c_def:
                            print(f"电脑处于防御姿势回复sp")
                            time.sleep(1)
                            self.update_sp(self.computer, True, "S")
                            self.effective_attack_computer = 0
                    elif c_defend:
                        p_dmg = p_dmg//2
                        self.computer['hp'] -= p_dmg
                        print(f"玩家攻击腿部被电脑格挡了！仅造成{p_dmg}伤害")
                        time.sleep(1)
                        print(f"电脑格挡成功回复sp")
                        time.sleep(1)
                        self.update_sp(self.computer, True, "M")
                        self.effective_attack_computer = 0
                        self.effective_attack_player = 0
                        self.resume_effects(self.computer, 'unstable')
                    else: 
                        self.computer['hp'] -= p_dmg
                        self.effective_attack_player += 1
                        if self.computer['hegemony'] == True:
                            print(f"电脑霸体状态，不会不稳")
                            time.sleep(1)
                            print(f"玩家攻击腿部命中！造成{p_dmg}伤害")
                            time.sleep(1)
                            self.resume_effects(self.computer, 'hegemony')
                            self.resume_effects(self.computer, 'unstable')
                            print(f"玩家攻击成功回复sp")
                            time.sleep(1)
                            self.update_sp(self.player, True, "S")
                        else:
                            print(f"玩家攻击腿部命中！造成{p_dmg}伤害并使电脑不稳")
                            time.sleep(1)
                            print(f"玩家攻击成功回复sp")
                            time.sleep(1)
                            self.update_sp(self.player, True, "S")
                            if c_def:
                                print(f"电脑防御失败")
                                time.sleep(1)
                                self.update_sp(self.computer, True, "S")
                                self.effective_attack_computer = 0

        if c_leg:
            if self.computer['dizzy']:
                self.resume_effects(self.computer, 'dizzy')
                print(f"电脑因眩晕状态中断腿部攻击")
                time.sleep(1)
            else:
                if self.player['dodging']:
                    self.resume_effects(self.player, 'dodging')
                    print(f"电脑识破玩家的闪避，进行腿部攻击")
                    time.sleep(1)
                if cs_hit:
                    c_dmg, c_hit_rate = self.calculate_damage('sweep', is_skill=True)
                    if self.computer['unstable']:
                        c_hit_rate -= 20
                        self.resume_effects(self.computer, 'unstable')
                    c_hit = random.random() * 100 < c_hit_rate
                    if not c_hit:
                        print(f"电脑腿部技能未命中！")
                        time.sleep(1)
                        self.effective_attack_computer = 0
                        self.resume_effects(self.player, 'prone')
                        if p_defend:
                            print(f"玩家通过防御完美躲过攻击")
                            time.sleep(1)
                            self.update_sp(self.player, True, "L")
                            self.effective_attack_player = 0
                        elif p_def:
                            print(f"玩家处于防御姿势回复sp")
                            time.sleep(1)
                            self.update_sp(self.player, True, "S")
                            self.effective_attack_player = 0
                    elif p_defend:
                        c_dmg = c_dmg//2
                        self.player['hp'] -= c_dmg
                        print(f"电脑扫腿被玩家格挡了！仅造成{c_dmg}伤害")
                        time.sleep(1)
                        print(f"玩家格挡成功回复sp")
                        time.sleep(1)
                        self.update_sp(self.player, True, "L")
                        self.effective_attack_computer = 0
                        self.effective_attack_player = 0
                        self.resume_effects(self.player, 'prone')
                    else: 
                        self.player['hp'] -= c_dmg
                        self.effective_attack_computer += 1
                        if self.player['hegemony'] == True:
                            print(f"玩家霸体状态，不会倒地")
                            time.sleep(1)
                            print(f"电脑扫腿命中！造成{c_dmg}伤害")
                            time.sleep(1)
                            self.resume_effects(self.player, 'hegemony')
                            self.resume_effects(self.player, 'prone')
                        else:
                            print(f"电脑扫腿命中！造成{c_dmg}伤害并使玩家倒地")
                            time.sleep(1)
                            if p_def:
                                print(f"玩家防御失败")
                                time.sleep(1)
                                self.update_sp(self.player, True, "S")
                                self.effective_attack_player = 0
                else:
                    c_dmg, c_hit_rate = self.calculate_damage('legs', is_skill=False)
                    if self.computer['unstable']:
                        c_hit_rate -= 20
                        self.resume_effects(self.computer, 'unstable')
                    c_hit = random.random() * 100 < c_hit_rate
                    if not c_hit:
                        print(f"电脑腿部攻击未命中！")
                        time.sleep(1)
                        self.effective_attack_computer = 0
                        self.resume_effects(self.player, 'unstable')
                        if p_defend:
                            print(f"玩家通过防御完美躲过攻击")
                            time.sleep(1)
                            self.update_sp(self.player, True, "L")
                            self.effective_attack_player = 0
                        elif p_def:
                            print(f"玩家处于防御姿势回复sp")
                            time.sleep(1)
                            self.update_sp(self.player, True, "S")
                            self.effective_attack_player = 0
                    elif p_defend:
                        c_dmg = c_dmg//2
                        self.player['hp'] -= c_dmg
                        print(f"电脑攻击腿部被玩家格挡了！仅造成{c_dmg}伤害")
                        time.sleep(1)
                        print(f"玩家格挡成功回复sp")
                        time.sleep(1)
                        self.update_sp(self.player, True, "M")
                        self.effective_attack_player = 0
                        self.effective_attack_computer = 0
                        self.resume_effects(self.player, 'unstable')
                    else: 
                        self.player['hp'] -= c_dmg
                        self.effective_attack_computer += 1
                        if self.player['hegemony'] == True:
                            print(f"玩家霸体状态，不会不稳")
                            time.sleep(1)
                            print(f"电脑攻击腿部命中！造成{c_dmg}伤害")
                            time.sleep(1)
                            self.resume_effects(self.player, 'hegemony')
                            self.resume_effects(self.player, 'unstable')
                            print(f"电脑攻击成功回复sp")
                            time.sleep(1)
                            self.update_sp(self.computer, True, "S")
                        else:
                            print(f"电脑攻击腿部命中！造成{c_dmg}伤害并使玩家不稳")
                            time.sleep(1)
                            print(f"电脑攻击成功回复sp")
                            time.sleep(1)
                            self.update_sp(self.computer, True, "S")
                            if p_def:
                                print(f"玩家防御失败")
                                time.sleep(1)
                                self.update_sp(self.player, True, "S")
                                self.effective_attack_player = 0

    def handle_normal_attack(self, p_action, c_action):
        """处理普通攻击"""
        if self.player['dodging'] == True or self.computer['dodging'] == True:
            self.fun_dodge(p_action, c_action)
            return
        
        p_attack = p_action.get('attack_type')
        c_attack = c_action.get('attack_type')

        p_defend = p_action.get('defend_type') == c_attack
        c_defend = c_action.get('defend_type') == p_attack

        p_def = p_action.get('type') == 'defend'
        c_def = c_action.get('type') == 'defend'

        if self.player['dizzy']:
                self.resume_effects(self.player, 'dizzy')
                print(f"玩家因眩晕状态中断攻击")
                time.sleep(1)
                self.effective_attack_player = 0
        else:
            if p_attack in ['head','body']:
                p_dmg, p_hit_rate = self.calculate_damage(p_attack)
                if self.player['unstable']:
                    p_hit_rate -= 20
                    self.resume_effects(self.player, 'unstable')
                p_hit = random.random() * 100 < p_hit_rate
                if p_hit:
                    if c_defend:
                        p_dmg = p_dmg//2
                        self.computer['hp'] -= p_dmg
                        print(f"玩家普攻被电脑格挡了！仅造成{p_dmg}伤害")
                        time.sleep(1)
                        print(f"电脑格挡成功回复sp")
                        time.sleep(1)
                        self.update_sp(self.computer, True, "M")
                        self.effective_attack_player = 0
                        self.effective_attack_computer = 0
                    else:
                        self.computer['hp'] -= p_dmg
                        self.effective_attack_player += 1
                        print(f"玩家攻击{p_attack}命中！造成{p_dmg}伤害并回复sp")
                        time.sleep(1)
                        self.update_sp(self.player, True, "S")
                        if c_def:
                            print(f"电脑防御失败！")
                            time.sleep(1)
                            self.update_sp(self.computer, True, "S")
                            self.effective_attack_computer = 0
                else:
                    print(f"玩家攻击{p_attack}未命中！")
                    time.sleep(1)
                    self.effective_attack_player = 0
                    if c_defend:
                        print(f"电脑通过防御完美躲过攻击")
                        time.sleep(1)
                        self.update_sp(self.computer, True, "L")
                        self.effective_attack_computer = 0
                    elif c_def:
                        print(f"电脑处于防御姿势回复sp")
                        time.sleep(1)
                        self.update_sp(self.computer, True, "S")
                        self.effective_attack_computer = 0

        if self.computer['dizzy']:
                self.resume_effects(self.computer, 'dizzy')
                print(f"电脑因眩晕状态中断攻击")
                time.sleep(1)
                self.effective_attack_computer = 0
        else:
            if c_attack in ['head','body']:
                c_dmg, c_hit_rate = self.calculate_damage(c_attack)
                if self.computer['unstable']:
                    c_hit_rate -= 20
                    self.resume_effects(self.computer, 'unstable')
                c_hit = random.random() * 100 < c_hit_rate
                if c_hit:
                    if p_defend:
                        c_dmg = c_dmg//2
                        self.player['hp'] -= c_dmg
                        print(f"电脑普攻被玩家格挡了！仅造成{c_dmg}伤害")
                        time.sleep(1)
                        print(f"玩家格挡成功回复sp")
                        time.sleep(1)
                        self.update_sp(self.player, True, "M")
                        self.effective_attack_player = 0
                        self.effective_attack_computer = 0
                    else:
                        self.player['hp'] -= c_dmg
                        self.effective_attack_computer += 1
                        print(f"电脑攻击{c_attack}命中！造成{c_dmg}伤害并回复sp")
                        time.sleep(1)
                        self.update_sp(self.computer, True, "S")
                        if p_def:
                            print(f"玩家防御失败！")
                            time.sleep(1)
                            self.update_sp(self.player, True, "S")
                            self.effective_attack_player = 0
                else:
                    print(f"电脑攻击{c_attack}未命中！")
                    time.sleep(1)
                    self.effective_attack_computer = 0
                    if p_defend:
                        print(f"玩家通过防御完美躲过攻击")
                        time.sleep(1)
                        self.update_sp(self.player, True, "L")
                        self.effective_attack_player = 0
                    elif p_def:
                        print(f"玩家处于防御姿势回复sp")
                        time.sleep(1)
                        self.update_sp(self.player, True, "S")
                        self.effective_attack_player = 0

        if p_def and c_def:
            print(f"双方同时防守，观察对方行动")
            self.update_sp(self.player, True, "S")
            self.update_sp(self.computer, True, "S")
            self.effective_attack_player = 0
            self.effective_attack_computer = 0


    def update_sp(self, character, success, size = "S"):
        """更新技能槽和技能点"""
        if  success:
            if size == "L":
                character['sp'] += 100
            elif size == "M":
                character['sp'] += 50
            else:
                character['sp'] += 20
            if character['sp'] >= 100:
                character['skill_points'] = min(3, character['skill_points'] + 1)
                character['sp'] -= 100
                print(f"{character['user']}获得1个技能点！当前技能点：{character['skill_points']}")
                time.sleep(1)

    def update_status(self):
        #清空前动作遗漏状态
        self.resume_effects(self.player, 'unstable')
        self.resume_effects(self.computer, 'unstable')
        self.resume_effects(self.player, 'hegemony')
        self.resume_effects(self.computer, 'hegemony')
        self.resume_effects(self.player, 'dodging')
        self.resume_effects(self.computer, 'dodging')

        if self.effective_attack_player >= 3:
            self.apply_effects(self.computer, 'dizzy')
            print(f"玩家连击造成电脑眩晕状态")
            time.sleep(1)
            self.effective_attack_player = 0

        if self.effective_attack_computer >= 3:
            self.apply_effects(self.player, 'dizzy')
            print(f"电脑连击造成玩家眩晕状态")
            time.sleep(1)
            self.effective_attack_computer = 0    

    def check_win(self):
        """检查胜负条件"""
        if self.player['hp'] <= 0 and self.computer['hp'] <= 0:
            print("\n【平局】双方HP同时归零！")
            time.sleep(1)
            return True
        if self.player['hp'] <= 0:
            print("\n【失败】玩家HP归零！")
            time.sleep(1)
            return True
        if self.computer['hp'] <= 0:
            print("\n【胜利】电脑HP归零！")
            time.sleep(1)
            return True
        return False

    def start_game(self):
        """主游戏循环"""
        while self.player['hp'] > 0 and self.computer['hp'] > 0:
            self.display_status()

            for i in range(2):
                # 玩家选择动作
                player_actions = []
                if self.player['prone']:
                    player_actions.append({'type': 'rise'}) 
                    self.resume_effects(self.player, 'prone')
                    print(f"\n动作{i+1}玩家倒地，准备起身")
                    time.sleep(1)
                    self.effective_attack_player = 0
                elif self.player['dizzy']:
                    player_actions.append({'type': 'dizzy'}) 
                    self.resume_effects(self.player, 'dizzy')
                    print(f"\n动作{i+1}玩家眩晕，无法动弹")
                    time.sleep(1)
                    self.effective_attack_player = 0
                else:
                    print(f"\n请选择动作{i+1}:")
                    print("1.攻击 2.防守", end="")
                    if self.player['skill_points'] > 0:
                        print(" 3.技能")
                    else:
                        print()

                    while True:
                        choice = input("> ")
                        if choice == '1':
                            action = {'type': 'attack'}
                            print("请选择攻击部位:")
                            print("1.头部 2.身体 3.腿部")
                            part = input("> ")
                            action['attack_type'] = ['head', 'body', 'legs'][int(part)-1]
                            break
                        elif choice == '2':
                            action = {'type': 'defend'}
                            print("请选择防御部位:")
                            print("1.头部 2.身体 3.腿部")
                            part = input("> ")
                            action['defend_type'] = ['head', 'body', 'legs'][int(part)-1]
                            break
                        elif choice == '3' and self.player['skill_points'] > 0:
                            action = {'type': 'skill'}
                            # 技能选项映射表（支持多种输入方式）
                            skill_options = {
                                '1': 'heavy',
                                '2': 'dodge',
                                '3': 'elbow',
                                '4': 'sweep',
                                'heavy': 'heavy',
                                'dodge': 'dodge',
                                'elbow': 'elbow',
                                'sweep': 'sweep',
                                '重击': 'heavy',
                                '闪避': 'dodge',
                                '肘击': 'elbow',
                                '扫腿': 'sweep'
                            }
                        
                            prompt = "请选择技能（1.重击<heavy> / 2.闪避<dodge> / 3.肘击<elbow> / 4.扫腿<sweep>）: "
                            user_input = input(prompt).strip()
                        
                            skill = skill_options.get(user_input.lower())
                            if not skill:
                                print("无效技能！请重新选择！")
                                print(f"\n请选择动作{i+1}:")
                                print("1.攻击 2.防守 3.技能")
                                continue
                        
                            action['type'] = skill
                            break
                        else:
                            print("无效选择，请重新输入！")

                    player_actions.append(action)

                # 电脑随机动作
                computer_actions = []

                if self.computer['prone']:
                    computer_actions.append({'type': 'rise'})
                    self.resume_effects(self.computer, 'prone')
                    print(f"\n动作{i+1}电脑倒地，准备起身")
                    time.sleep(1)
                    self.effective_attack_computer = 0
                elif self.computer['dizzy']:
                    computer_actions.append({'type': 'dizzy'}) 
                    self.resume_effects(self.computer, 'dizzy')
                    print(f"\n动作{i+1}电脑眩晕，无法动弹")
                    time.sleep(1)
                    self.effective_attack_computer = 0
                else:
                    available_actions = self.get_available_actions(self.computer)
                    action_type = random.choice(available_actions)
                    action_options = {
                        'attack': '攻击',
                        'defend': '防守',
                        'skill': '技能'
                        }
                    pc_action = action_options.get(action_type.lower()) 
                    print(f"\n电脑选择动作{i+1}:{pc_action}")
                    time.sleep(1)
                    action = {'type': action_type}

                    if action_type == 'attack':
                        part = random.choice(['head', 'body', 'legs'])
                        part_options = {
                            'head': '头部',
                            'body': '身体',
                            'legs': '腿部'
                            }
                        pc_part = part_options.get(part.lower()) 
                        print(f"\n电脑攻击玩家:{pc_part}")
                        time.sleep(1)
                        action['attack_type'] = part

                    if action_type == 'defend':
                        part = random.choice(['head', 'body', 'legs'])
                        part_options = {
                            'head': '头部',
                            'body': '身体',
                            'legs': '腿部'
                            }
                        pc_part = part_options.get(part.lower()) 
                        print(f"\n电脑防御玩家:{pc_part}方位攻击")
                        time.sleep(1)
                        action['defend_type'] = part

                    elif action_type == 'skill':
                        skill = random.choice(['heavy', 'dodge', 'elbow', 'sweep'])
                        pc_skill_options = {
                            'heavy': '重击',
                            'dodge': '闪避',
                            'elbow': '肘击',
                            'sweep': '扫腿',
                            }
                        pc_skill = pc_skill_options.get(skill.lower()) 
                        print(f"\n电脑选择技能:{pc_skill}")
                        time.sleep(1)
                        action['type'] = skill

                    computer_actions.append(action)

                self.execute_action(player_actions[0], computer_actions[0], i+1)
                if self.check_win():
                    return
                time.sleep(1)
                #更新当前状态
                self.update_status()

            self.round += 1

    def display_status(self):
        """显示当前状态"""
        print(f"\n第{self.round}回合")
        print(f"玩家状态：HP:{self.player['hp']} | SP:{self.player['sp']} | 技能点:{self.player['skill_points']}")
        print(f"电脑状态：HP:{self.computer['hp']} | SP:{self.computer['sp']} | 技能点:{self.computer['skill_points']}")
        print(f"当前状态：玩家{'倒地' if self.player['prone'] else '眩晕' if self.player['dizzy'] else ''} 电脑{'倒地' if self.computer['prone'] else '眩晕' if self.computer['dizzy'] else ''}")
        time.sleep(1)

# 启动游戏
if __name__ == "__main__":
    game = FightingGame()
    game.start_game()

    while True:
        print(f"\n请选择:1.退出    ；2.下一局")
        choice = input("> ")
        if choice == '1':
            print(f"\n欢迎下次再来")
            break
        elif choice == '2':
            print(f"\n新的一局开始！")
            game = FightingGame()
            game.start_game()
        else:
            print("无效选择，请重新输入！")
